package com.dj.mingkong.common.util;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author CG
 * data 2021/10/28 10:00
 */
@Slf4j
public class ThreadPoolUtils {

    private static final ThreadPoolExecutor THREAD_POOL_EXECUTOR;

    static {

        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("-->log-pool-%d").setUncaughtExceptionHandler((t, e) -> {
            //todo 发送 rocketmq
            log.error("execute thread name {}, error", t.getName(), e);
        }).build();
        THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(10, 20, 60,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(10_0000), threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
    }

    public static ThreadPoolExecutor getThreadPool() {
        return THREAD_POOL_EXECUTOR;
    }

}
